package com.ttedu.action.manage.chaptertips;

import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.ttedu.base.BaseAction;
import com.ttedu.database.model.chaptertips;
import com.ttedu.database.service.ChaptertipsService;
import com.ttedu.util.Validator;

public class ChaptertipsAction extends BaseAction{
	public chaptertips chapter=new chaptertips();
	private String showid;
	@Autowired
	private ChaptertipsService chaptertipsservice;
	//展示
	@Action(value = "/cas/chaptertips_index", results = { @Result(name = "success", location = "/WEB-INF/html/manage/chaptertips/index.jsp") })
	public String chaptertips_index() {
		return "success";
	}
	//添加
	@Action(value = "/cas/chaptertips_add", results = { @Result(name = "success", location = "/WEB-INF/html/manage/chaptertips/add.jsp") })
	public String chaptertips_add() {
		String treeNodeid=request.getParameter("treeNodeid");
		if(Validator.isNull(treeNodeid)){
			treeNodeid="0";
		}else{
			chaptertips chapterone=chaptertipsservice.selectByPrimaryKey(Integer.valueOf(treeNodeid));
			request.setAttribute("parentname", chapterone.getName());
			chapter.setParentid(Integer.valueOf(treeNodeid));
		}
		request.setAttribute("treeNodeid", treeNodeid);
		return "success";
	}
	//显示
	@Action(value = "/cas/chaptertips_show", results = { @Result(name = "success", location = "/WEB-INF/html/manage/chaptertips/show.jsp") })
	public String chaptertips_show() {
		String id=request.getParameter("id");
		chapter=chaptertipsservice.selectByPrimaryKey(Integer.valueOf(id));
		int treeNodeid=chapter.getParentid();
		if(treeNodeid==0){
			treeNodeid=0;
		}else{
			chaptertips chapterone=chaptertipsservice.selectByPrimaryKey(treeNodeid);
			request.setAttribute("parentname", chapterone.getName());
		}
		request.setAttribute("treeNodeid", treeNodeid);
		return "success";
	}
	//保存或更新
	@Action(value = "/cas/chaptertips_updateorsave", results = { @Result(name = "success",type="redirect", location = "/cas/chaptertips_show.action?id=${showid}") })
	public String chaptertips_updateorsave() {
		if(Validator.isNotNull(chapter.getId())){
			chapter.setUpdatedAt(new Date());
			chaptertipsservice.updateByPrimaryKey(chapter);
		}
		//保存
		else{
			chapter.setCreatedAt(new Date());
			chapter.setUpdatedAt(new Date());
			if(Validator.isNull(chapter.getParentid())){
				chapter.setParentid(0);
			}
			chaptertipsservice.insert(chapter);
		}
		showid=String.valueOf(chapter.getId());
		return "success";
	}
	//删除
	@Action(value = "/cas/chaptertips_delete", results = { @Result(name = "success", location = "/WEB-INF/html/manage/chaptertips/success.jsp") })
	public String chaptertips_delete() {
		chaptertipsservice.deleteByPrimaryKey(chapter.getId());
		return "success";
	}
	//编辑
	@Action(value = "/cas/chaptertips_edit", results = { @Result(name = "success", location = "/WEB-INF/html/manage/chaptertips/edit.jsp") })
	public String chaptertips_edit() {
		String id=request.getParameter("id");
		chapter=chaptertipsservice.selectByPrimaryKey(Integer.valueOf(id));
		int treeNodeid=chapter.getParentid();
		if(treeNodeid==0){
			treeNodeid=0;
		}else{
			chaptertips chapterone=chaptertipsservice.selectByPrimaryKey(treeNodeid);
			request.setAttribute("parentname", chapterone.getName());
		}
		request.setAttribute("treeNodeid", treeNodeid);
		return "success";
	}
	
	public chaptertips getChapter() {
		return chapter;
	}
	public void setChapter(chaptertips chapter) {
		this.chapter = chapter;
	}
	public String getShowid() {
		return showid;
	}
	public void setShowid(String showid) {
		this.showid = showid;
	}
	
}
